package com.jsz.peini.controller.app;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jsz.peini.bean.seller.SellerDiscount;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.CommonUtils;
import com.jsz.peini.mapper.seller.SellerDiscountMapper;
import com.jsz.peini.service.seller.SellerInfoService;

@Controller
public class SellerDiscountController {
	private static final Logger logger = LoggerFactory.getLogger(SellerDiscountController.class);
	@Resource
	private SellerDiscountMapper sellerDiscountMapper;
	@Resource
	private SellerInfoService sellerInfoService;

	@ResponseBody
	@RequestMapping(value="/getSellerDiscount")
	public Object getSellerDiscount(HttpServletRequest request,
			@RequestParam(value="sellerInfoId") Integer sellerInfoId
			){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		
		try {
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			String dateStr = sdf.format(d);
			
			SellerDiscount sellerDiscount = new SellerDiscount();
			sellerDiscount.setSellerInfoId(sellerInfoId);
			sellerDiscount.setNowNum(CommonUtils.getWeekNum());
			sellerDiscount.setNowTime(dateStr);
			SellerDiscount discountMap = sellerInfoService.getSellerDiscount(sellerDiscount);
			
			if (discountMap != null) {
				JSONObject object = new JSONObject();
				if(discountMap.getGoldNum() != null && discountMap.getGoldNum() != 100){
					BigDecimal goldNum = new BigDecimal(discountMap.getGoldNum());
					object.put("gold", goldNum);
				}else{
					object.put("gold", 100);
				}
				
				if(discountMap.getWeixinNum() != null && discountMap.getWeixinNum() != 100){
					BigDecimal wxNum = new BigDecimal(discountMap.getWeixinNum());
					object.put("wxpay", wxNum);
				}else{
					object.put("wxpay", 100);
				}
				
				if(discountMap.getZfbNum() != null && discountMap.getZfbNum() != 100){
					BigDecimal aliNum = new BigDecimal(discountMap.getZfbNum());
					object.put("alipay",aliNum);
				}else{
					object.put("alipay", 100);
				}
				
				infoMap.put("date", object);
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", Constants.SUCCESS_DESC);
				return infoMap;
			}else{
				JSONObject object = new JSONObject();
				object.put("gold", 100);
				object.put("alipay", 100);
				object.put("wxpay", 100);
				infoMap.put("date", object);
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", Constants.SUCCESS_DESC);
				return infoMap;
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	/*public Object getSellerDiscount(HttpServletRequest request,
			@RequestParam(value="sellerInfoId") Integer sellerInfoId
			){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try{
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			String dateStr = sdf.format(d);
			
			int week = DateTimeUtils.getWeek();
			SellerDiscountExample example = new SellerDiscountExample();
			Criteria criteria = example.createCriteria();
			criteria.andStartNumLessThanOrEqualTo(week);
			criteria.andEndNumGreaterThanOrEqualTo(week);
			criteria.andSellerInfoIdEqualTo(sellerInfoId);
			List<SellerDiscount> list = sellerDiscountMapper.selectByExample(example);
			if(list.size() > 0){
				for (SellerDiscount dis : list) {
					String frontStr = DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd");
					String startTime = frontStr+" "+dis.getStartTime();
					String endTime = frontStr+" "+dis.getEndTime();
					
					//营业时间推后一小时
					Calendar cc = Calendar.getInstance();
					cc.setTime(DateTimeUtils.formatString2Date(endTime, "yyyy-MM-dd HH:mm"));
					cc.set(Calendar.HOUR_OF_DAY, cc.get(Calendar.HOUR_OF_DAY)+1);
					endTime = DateTimeUtils.formatDateTime2LongString(cc.getTime());
					
					Date now = new Date();
					String nowStr = DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd HH:mm");
					if(DateTimeUtils.formatString2Date(startTime, "yyyy-MM-dd HH:mm").before(now) && DateTimeUtils.formatString2Date(endTime, "yyyy-MM-dd HH:mm").after(now)){
						JSONObject object = new JSONObject();
						object.put("gold", dis.getGoldNum());
						object.put("alipay", dis.getZfbNum());
						object.put("wxpay", dis.getWeixinNum());
						infoMap.put("date", object);
						infoMap.put("resultCode", Constants.SUCCESS);
						infoMap.put("resultDesc", Constants.SUCCESS_DESC);
						return infoMap;
					}else if(startTime.equals(nowStr) || endTime.equals(nowStr)){
						JSONObject object = new JSONObject();
						object.put("gold", dis.getGoldNum());
						object.put("alipay", dis.getZfbNum());
						object.put("wxpay", dis.getWeixinNum());
						infoMap.put("date", object);
						infoMap.put("resultCode", Constants.SUCCESS);
						infoMap.put("resultDesc", Constants.SUCCESS_DESC);
						return infoMap;
					}else{
						Date start = DateTimeUtils.formatString2Date(startTime, "yyyy-MM-dd HH:mm");
						Date end = DateTimeUtils.formatString2Date(endTime, "yyyy-MM-dd HH:mm");
						if(end.before(start)){
							Date startCenter = DateTimeUtils.formatString2Date(DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd")+" 23:59:59", "yyyy-MM-dd HH:mm:ss");
							Calendar calendar = Calendar.getInstance();
							calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加1  
							Date endCenter = DateTimeUtils.formatString2Date(DateTimeUtils.formatDateTime2String(calendar.getTime(), "yyyy-MM-dd")+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
							
							if(DateTimeUtils.formatString2Date(startTime, "yyyy-MM-dd HH:mm").before(now) && startCenter.after(now)){
								JSONObject object = new JSONObject();
								object.put("gold", dis.getGoldNum());
								object.put("alipay", dis.getZfbNum());
								object.put("wxpay", dis.getWeixinNum());
								infoMap.put("date", object);
								infoMap.put("resultCode", Constants.SUCCESS);
								infoMap.put("resultDesc", Constants.SUCCESS_DESC);
								return infoMap;
							}else if(endCenter.before(endCenter) && DateTimeUtils.formatString2Date(endTime, "yyyy-MM-dd HH:mm").after(now)){
								JSONObject object = new JSONObject();
								object.put("gold", dis.getGoldNum());
								object.put("alipay", dis.getZfbNum());
								object.put("wxpay", dis.getWeixinNum());
								infoMap.put("date", object);
								infoMap.put("resultCode", Constants.SUCCESS);
								infoMap.put("resultDesc", Constants.SUCCESS_DESC);
								return infoMap;
							}else if((DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd")+" 23:59").equals(nowStr) || (DateTimeUtils.formatDateTime2String(calendar.getTime(), "yyyy-MM-dd")+" 00:00").equals(nowStr)){
								JSONObject object = new JSONObject();
								object.put("gold", dis.getGoldNum());
								object.put("alipay", dis.getZfbNum());
								object.put("wxpay", dis.getWeixinNum());
								infoMap.put("date", object);
								infoMap.put("resultCode", Constants.SUCCESS);
								infoMap.put("resultDesc", Constants.SUCCESS_DESC);
								return infoMap;
							}
						}
						
						JSONObject object = new JSONObject();
						object.put("gold", 100);
						object.put("alipay", 100);
						object.put("wxpay", 100);
						infoMap.put("date", object);
						infoMap.put("resultCode", Constants.SUCCESS);
						infoMap.put("resultDesc", Constants.SUCCESS_DESC);
						return infoMap;
					}
				}
			}else{
				JSONObject object = new JSONObject();
				object.put("gold", 100);
				object.put("alipay", 100);
				object.put("wxpay", 100);
				infoMap.put("date", object);
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", Constants.SUCCESS_DESC);
				return infoMap;
			}
		}catch(Exception e){
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}*/
}
